package com.mida.app.knw.controller;

import com.mida.app.knw.service.KnwConceptualService;
import com.mida.dms.knw.api.KnwConceptualApi;
import com.mida.dms.knw.entity.KnwConceptualDomain;
import com.mida.dms.knw.entity.vo.KnwCodingConceptualDomainPageVo;
import com.mida.dms.knw.entity.vo.KnwConceptualDomainVo;
import com.mida.dms.knw.entity.vo.KnwConceptualVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: KnwConceptualController
 * @Description: 概念controller
 * @Author: wang xiao le
 * @Date: 2021/6/21 17:41
 **/
@RestController
@RequestMapping("/knwConceptual")
@Slf4j
@CrossOrigin
public class KnwConceptualController {

    @Autowired
    private KnwConceptualApi knwConceptualApi;

    @Autowired
    private KnwConceptualService knwConceptualService;

    /**
     * 1、新增概念基本信息
     * （form）
     *
     * @param ConceptFsn         概念FSN
     * @param conceptType        概念类型
     * @param isConceptualDomain 是否有概念域
     * @param status             状态
     * @return
     */
    @PostMapping(value = "/addKnwConceptual")
    public ActionResult add(@RequestParam(value = "ConceptFsn") String ConceptFsn,
                            @RequestParam(value = "conceptType") Long conceptType,
                            @RequestParam(value = "isConceptualDomain") Long isConceptualDomain,
                            @RequestParam(value = "status") Long status) {
        return knwConceptualApi.add(ConceptFsn, conceptType, isConceptualDomain, status);
    }


    /**
     * /2、 更新概念
     * （form）
     *
     * @param conceptualSoid     概念soid
     * @param ConceptFsn         概念FSN
     * @param conceptType        概念类型
     * @param isConceptualDomain 是否有概念域
     * @param status             状态
     * @return
     */
    @PostMapping("/updateKnwConceptual")
    ActionResult update(@RequestParam(value = "conceptualSoid") Long conceptualSoid,
                        @RequestParam(value = "ConceptFsn") String ConceptFsn,
                        @RequestParam(value = "conceptType") Long conceptType,
                        @RequestParam(value = "isConceptualDomain") Long isConceptualDomain,
                        @RequestParam(value = "status") Long status) {
        return knwConceptualApi.update(conceptualSoid, ConceptFsn, conceptType, isConceptualDomain, status);
    }

    /**
     * /3、 删除概念
     *
     * @param soid
     * @return
     */
    @GetMapping("/deleteKnwConceptual")
    public ActionResult delete(@RequestParam(value = "soid") long soid) {
        return knwConceptualApi.delete(soid);
    }

    /**
     * 4、获取概念
     *
     * @param soid
     * @return
     */
    @GetMapping("/getKnwConceptual")
    ListResult<KnwConceptualVo> get(@RequestParam(value = "soid") long soid) {
        return knwConceptualApi.get(soid);
    }

    /**
     * 5、概念分页查询
     *
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @param searChData  搜索关键词
     * @param conceptType 概念类型
     * @return
     */
    @PostMapping(value = "/findPage")
    ListResult<PageResultVo<KnwConceptualVo>> findPage(@RequestParam(value = "currentPage") Long currentPage,
                                                       @RequestParam(value = "pageSize") Long pageSize,
                                                       @RequestParam(value = "searChData") String searChData,
                                                       @RequestParam(value = "conceptType") Long conceptType) {
        return knwConceptualService.findPage(currentPage, pageSize, searChData, conceptType);
    }

    /**
     * 6、查询全部概念
     *
     * @param searChData  搜索关键词
     * @param conceptType 概念类型
     * @return
     */
    @GetMapping(value = "/findAll")
    public ListResult<List<KnwConceptualVo>> findAll(@RequestParam(value = "searChData", required = false) String searChData,
                                                     @RequestParam(value = "conceptType", required = false) Long conceptType) {
        return knwConceptualApi.findAll(searChData, conceptType);
    }

    /**
     * 7、校验概念FSN重复性
     *
     * @param name 概念名称
     */
    @GetMapping(value = "/checkName")
    public ActionResult checkName(@RequestParam(value = "name", required = true) String name) {
        return knwConceptualApi.checkName(name);
    }

    /**
     * 8、概念域绑定编码体系查询
     *
     * @param conceptualSoid 概念soid
     */
    @GetMapping(value = "/getKnwConceptualDomain")
    public ListResult<List<KnwConceptualDomainVo>> getKnwConceptualDomain(@RequestParam(value = "conceptualSoid", required = true) long conceptualSoid) {
        return knwConceptualService.getKnwConceptualDomain(conceptualSoid);
    }

    /**
     * 9、概念域查询所有编码体系
     *
     * @param domainSoid 概念域soid
     * @return
     */
    @GetMapping(value = "/getCodingDomainAll")
    public ListResult<List<KnwCodingConceptualDomainPageVo>> getCodingDomainAll(@RequestParam(value = "domainSoid", required = true) Long domainSoid) {
        return knwConceptualService.getCodingDomainAll(domainSoid);
    }

    /**
     * 10、概念域分页查询
     *
     *
     * @param domainSoid  概念域soid
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @return
     */
    @PostMapping(value = "/getCodingDomainPage")
    public ListResult<PageResultVo<KnwCodingConceptualDomainPageVo>> getCodingDomainPage(@RequestParam(value = "domainSoid") Long domainSoid,
                                                                                         @RequestParam(value = "currentPage") Long currentPage,
                                                                                         @RequestParam(value = "pageSize") Long pageSize) {
        return knwConceptualService.getCodingDomainPage(domainSoid, currentPage, pageSize);
    }

    /**
     * 11、绑定编码体系业务处理
     *
     * @param domainSoid              概念域soid
     * @param derailCodingSystemSoids 编码体系soid集合
     * @return
     */
    @PostMapping(value = "/addDerailCodingSystem")
    public ActionResult addDerailCodingSystem(@RequestParam(value = "domainSoid") long domainSoid,
                                              @RequestParam(value = "derailCodingSystemSoids") String derailCodingSystemSoids) {
        return knwConceptualApi.addDerailCodingSystem(domainSoid, derailCodingSystemSoids);
    }

    /**
     * 12、解绑业务处理
     *
     * @param codingDomainSoid 概念域编码体系关系SOID
     * @return
     */
    @PostMapping(value = "/deleteDerailCodingSystem")
    public ActionResult deleteDerailCodingSystem(@RequestParam(value = "codingDomainSoid") long codingDomainSoid) {
        return knwConceptualApi.deleteDerailCodingSystem(codingDomainSoid);
    }

    /**
     * 13、更新首选标识业务处理
     *
     * @param domainSoid       概念域SOID
     * @param codingDomainSoid 概念域编码体系关系SOID
     * @return
     */
    @PostMapping(value = "/updateIsPreferred")
    public ActionResult update(@RequestParam(value = "codingDomainSoid") long codingDomainSoid,
                               @RequestParam(value = "domainSoid") long domainSoid) {
        return knwConceptualApi.update(codingDomainSoid, domainSoid);
    }

    /**
     * 14、查询所有概念域
     *
     * @return
     */
    @GetMapping(value = "/getAllCodingDomain")
    public ListResult<List<KnwConceptualDomain>> getAllCodingDomain() {
        return knwConceptualApi.getAllCodingDomain();
    }

    /**
     * 15、更新概念状态
     *
     * @param
     * @param status         状态soid
     * @param conceptualSoid 概念soid
     * @return
     */
    @GetMapping("/updateStatus")
    public ActionResult updateStatus(@RequestParam(value = "status") Long status,
                                     @RequestParam(value = "conceptualSoid") Long conceptualSoid) {
        return knwConceptualApi.updateStatus(status, conceptualSoid);
    }

    /**
     * 16、批量上传
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public ActionResult uploadFile(@RequestPart("file") MultipartFile file) {

        ActionResult result = new ActionResult();
        try {
            result = knwConceptualApi.uploadFile(file);
        } catch (Exception e) {
            log.error("概念批量上传处理发生异常:" + e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("概念批量上传处理失败");
            return result;
        }
        return result;
    }
}
